﻿package stuff3D.render 
{
	import fab.geom.Line;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import stuff3D.meshes.Mesh;
	import stuff3D.meshes.Triangle;

	public class RenderMeshObject
	{
		public var vertices:Vector.<Number> = new Vector.<Number>;
		public var indices:Vector.<int> = new Vector.<int>;
		public var uvtData:Vector.<Number> = new Vector.<Number>;
		private var index:int;
		private var tmp_vertices:Vector.<Point>;
		private var mesh:Mesh;
		public var plotVector:Array;
		
		
		//projection sur ecran au point : (x * cameraFocalLength / cameraRelativeZPosition, y * cameraFocalLength / cameraRelativeZPosition)
			
		public function RenderMeshObject( container:Sprite, _mesh:Mesh ) 
		{
			mesh = _mesh;
			// utiliser plutot Utils3D.projectVectors
			
			/*
			vertices = new Vector.<Number>();
			var pt:Point;
			for each ( var dot:Vector3D in mesh.vertices )
			{
				pt = container.local3DToGlobal( dot );
				vertices.push( pt.x, pt.y );
			}
			indices = mesh.indices;
			uvtData = mesh.uvtData;
			*/
			
			tmp_vertices = new Vector.<Point>();
			for each ( var dot:Vector3D in mesh.vertices ) tmp_vertices.push( container.local3DToGlobal( dot ) );
			
			// update mesh.rendererMesh :
			for ( var i:int = 0; i < mesh.vertices.length; i++ ) mesh.rendererMesh.vertices[ i ] = container.transform.matrix3D.transformVector( mesh.vertices[ i ] );
			
			
			// ici : z-sorting de mesh.triangles
			var zMeshTriangles:Vector.<Triangle> = mesh.triangles.concat();			
			// essai de z sorting : http://flash.mediabox.fr/index.php?showtopic=97723
			
			zMeshTriangles.sort( zSorting );
			
			// draw normales
			/*
			plotVector = new Array();
			plotVector.push( getDoubleVector( zMeshTriangles[ 0 ] ) );
			plotVector.push( getDoubleVector( zMeshTriangles[ 1 ] ) );
			plotVector.push( getDoubleVector( zMeshTriangles[ 7 ] ) );
			*/
			
			// rendering
			
			index = 0;
			
			//renderTriangle( zMeshTriangles[ 1 ] );
			
			for each ( var triangle:Triangle in zMeshTriangles ) 
			{
				renderTriangle( triangle );
			}			
		}
		
		private function zSorting( t1:Triangle, t2:Triangle ):Number
		{
			var r1:Boolean = mesh.rendererMesh.wich_side( Main.camera, t1  );
			var r2:Boolean = mesh.rendererMesh.wich_side( mesh.rendererMesh.triangle_barycenter( t2 ), t1  );
			if ( r1 == r2 ) return 1;
			else return -1;
		}
		private function getTransformedVector3D( container:Sprite, v3D:Vector3D ):Vector3D
		{
			return container.transform.matrix3D.transformVector( v3D );
		}
		private function getDoubleVector( triangle:Triangle ):Object
		{
			var normaleScaled:Vector3D = mesh.normale( triangle );
			normaleScaled.scaleBy( 50 );
			var v1:Vector3D = mesh.vertices[ triangle.indices[ 0 ] ];
			var v2:Vector3D = normaleScaled.add( v1 );
			return { v1:v1, v2:v2 };
		}
		private function renderTriangle( triangle:Triangle ):void
		{
			vertices.push( 
			tmp_vertices[ triangle.indices[ 0 ] ].x, 
			tmp_vertices[ triangle.indices[ 0 ] ].y, 
			tmp_vertices[ triangle.indices[ 1 ] ].x, 
			tmp_vertices[ triangle.indices[ 1 ] ].y, 
			tmp_vertices[ triangle.indices[ 2 ] ].x,
			tmp_vertices[ triangle.indices[ 2 ] ].y
			);
			indices.push( index++, index++, index++ );
			uvtData.push( 
			mesh.uvdots[ triangle.uvdata[ 0 ] ].x,
			mesh.uvdots[ triangle.uvdata[ 0 ] ].y,
			mesh.uvdots[ triangle.uvdata[ 1 ] ].x, 
			mesh.uvdots[ triangle.uvdata[ 1 ] ].y, 
			mesh.uvdots[ triangle.uvdata[ 2 ] ].x,
			mesh.uvdots[ triangle.uvdata[ 2 ] ].y
			);
		}
	}

}